package com.school.mall.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school.mall.sys.entity.MallGoodsBrand;
import com.school.mall.sys.entity.MallGoodsColour;
import com.school.mall.sys.entity.MallGoodsSize;
import com.school.mall.sys.service.MallGoodsBrandService;
import com.school.mall.sys.service.MallGoodsColourService;
import com.school.mall.sys.service.MallGoodsSizeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import com.school.mall.common.result.R;
import com.school.mall.sys.entity.MallGoodsType;
import com.school.mall.sys.service.MallGoodsTypeService;
import com.school.mall.sys.entity.queryVo.MallGoodsTypeQueryVo;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商品类型表 前端控制器
 */
@RestController
@RequestMapping("/sys/mallGoodsType")
@Api(tags="商品类型表")
public class MallGoodsTypeController {

    @Autowired
    MallGoodsTypeService iMallGoodsTypeService;
 /*   @Autowired
    MallGoodsBrandService iMallGoodsBrandService;
    @Autowired
    MallGoodsColourService iMallGoodsColourService;
    @Autowired
    MallGoodsSizeService    iMallGoodsSizeService;*/

        /**
        * 查询商品类型表列表
        */
        @PostMapping("queryMallGoodsTypeList")
        @ApiOperation("商品类型表列表")
        public R queryMallGoodsTypeList(@RequestBody MallGoodsTypeQueryVo queryVo) {

            QueryWrapper<MallGoodsType> queryWrapper=new QueryWrapper<>();
            Page<MallGoodsType> page = iMallGoodsTypeService.page(queryVo.getPage(), queryWrapper);
            return R.ok().data("dataList",page);
        }

        /**
         * 保存或者更新商品类型表信息
         */
        @PostMapping("saveOrUpdateMallGoodsType")
        @ApiOperation("保存或者更新商品类型表信息")
        public R saveOrUpdateMallGoodsType(@RequestBody MallGoodsType entity) {
            boolean b =true;
            if(StringUtils.isEmpty(entity.getId())){
                b= iMallGoodsTypeService.save(entity);
            }else{
                b=iMallGoodsTypeService.updateById(entity);
            }
            return R.th(b);
        }


        /**
         * 根据id查询商品类型表信息
         */
        @GetMapping("getByIdMallGoodsType/{id}")
        @ApiOperation("根据id查询商品类型表信息")
        public R getByIdMallGoodsType(@PathVariable("id") String id) {
             MallGoodsType dataMallGoodsType=  iMallGoodsTypeService.getById(id);
             return R.ok().data("entity",dataMallGoodsType);

        }
        /**
         * 根据id删除商品类型表信息
         */
        @DeleteMapping("removeByIdMallGoodsType/{id}")
        @ApiOperation("根据id删除商品类型表信息")
        public R removeByIdMallGoodsType(@PathVariable("id") String id) {
            boolean effect= iMallGoodsTypeService.removeById(id);
            return R.th(effect);

        }
        @DeleteMapping("dropByIdsMallGoodsType/{ids}")
        @ApiOperation("根据id数组多选删除商品类型表信息")
        public R dropByIdsMallGoodsType(@PathVariable("ids") String ids){
            List<String> idList = Arrays.asList(ids.split(","));
            boolean b = iMallGoodsTypeService.deleteBatchIds(idList);
            return R.th(b);
        }

        @GetMapping("mallGoodsTypeList")
        public R mallGoodsTypeList(){
            List<MallGoodsType> list = iMallGoodsTypeService.list();
            List<Map<String, String>> collect = list.stream().map(res -> {
                Map<String, String> maps = new HashMap();
                maps.put("key", res.getTypeName());
                maps.put("value", String.valueOf(res.getId()));
                return maps;
            }).collect(Collectors.toList());
            /*  List<MallGoodsBrand> brandList = iMallGoodsBrandService.list();
            List<Map<String, String>> collect2 = brandList .stream().map(res -> {
                Map<String, String> maps = new HashMap();
                maps.put("key", res.getTypeName());
                maps.put("value", String.valueOf(res.getId()));
                return maps;
            }).collect(Collectors.toList());
            // 将list2追加到collect中
            collect.addAll(collect2);
            List<MallGoodsColour> colourList= iMallGoodsColourService.list();
            List<Map<String, String>> collect3 = colourList.stream().map(res -> {
                Map<String, String> maps = new HashMap();
                maps.put("key", res.getName());
                maps.put("value", String.valueOf(res.getId()));
                return maps;
            }).collect(Collectors.toList());
            // 将list3追加到collect中
            collect.addAll(collect3);
            List<MallGoodsSize> sizeList= iMallGoodsSizeService.list();
            List<Map<String, String>> collect4 = sizeList.stream().map(res -> {
                Map<String, String> maps = new HashMap();
                maps.put("key", res.getName());
                maps.put("value", String.valueOf(res.getId()));
                return maps;
            }).collect(Collectors.toList());
            // 将list4追加到collect中
            collect.addAll(collect4);
            System.out.println("colllect值为："+collect);*/
            return R.ok().data("dataList",collect);
        }

}

